function eye=tda_reader_05(filename,saveit)
	% function eye=tda_reader_05(filename)
	% send filenames as cell :  filename={files.name}';
	% reads in *.tda data from iScan and dynamically names fields based on what
	% was recorded
	% savit = [1 [0]] yes or no
	% 2006-12-18 bcoe@biomed.queensu.ca
	% 2008-06-19 bcoe: added cell & ui input
	% 2011-08-05 bcoe: multiple runs go in to an array of eye
	
	if nargin <1
		directory_name = uigetdir([],'locate the subject''s iscan directory') ;
		cd(directory_name)
		files=dir('*.tda');
		if isempty(files)
			disp(['no *.tda files found in ' cd])
			return
		end
		filename={files.name}';
	else
		if~iscell(filename)
			filename={filename};
		end
	end
	if nargin <2
		saveit =0;
	end
	if nargout==0
		saveit=1;
	end
	
	
	for j = 1:length(filename)
		fid=fopen(filename{j});
		if fid<1
			error(['can''t find/open ' filename])
		end
		tline='';
		current_run=1;
		%save_run=1;
		run_num=0;
		sum_num=0;
		linenum=0;
		eye=[];
		clear e;
		while current_run>0
			tline = fgetl(fid);
			if tline==-1; break;end
			linenum=linenum+1;
			str={};
			if isempty(tline)
				sum_num=0;run_num=0;
			else
				switch 1
					case strmatch('Runs Recorded:',tline)
						str=tab_delim_i(tline);
						Runs_Recorded=str2num(str{end});
						%                 if str2num(str{end})>1
						%                     disp('multiple runs in one file!')
						%                     save_run=input(['Which one do you want (1:' str{end} ')? ']);
						%                 end
						%             case strmatch('Samps Recorded:',tline)
						%                 str=tab_delim_i(tline);
						%                 Samps_Recorded=str2num(str{end})
					case strmatch('Run #	Date',tline)
						str=tab_delim_i(tline);
						run_num=1;
						Info_Table_header=str;
					case run_num>0 & run_num==str2num(tline(1:min(6,end)))
						str=tab_delim_i(tline);
						Info_Table(run_num,:)=str;
						run_num=run_num+1;
					case strmatch('Run #	Param',tline)
						str=tab_delim_i(tline);
						Summary_Table_header=str;
						Summary_Table={};
						sum_num=1;
					case sum_num>0
						str=tab_delim_i(tline);
						if length(str)==1
							sum_num=str2num(char(str));
							%Summary_Table(sum_num,1)=str;
						else
							Summary_Table(end+1,:)= str;
							Summary_Table(end,1)={(sum_num)};
						end
					case strmatch(['Run   ' num2str(current_run) ':'],tline)
						str=tab_delim_i(tline);
						data_headers=str(1:end-1);
						data_headers(1)={'Sample #'};
						tline = fgetl(fid);
						linenum=linenum+1;
						fred=[];
						for i=1:length(data_headers)
							fred=[fred '%f '];
						end
						data = textscan(fid, fred, str2num(Info_Table{1,4}) );
						if rem(str2num(Info_Table{current_run,4}),length(data_headers))
							%if some data colums are not long enough...
							for i = rem(str2num(Info_Table{1,4}),length(data_headers))+1:length(data)
								data{i}(length(data{1}))=data{i}(end);
							end
						end
						data=cell2mat(data);
						eye=[];
						eye.filename=filename;
						eye.run_summary=[Info_Table_header;Info_Table(current_run,:)];
						eye.data_summary=[Summary_Table_header;Summary_Table(cell2mat(Summary_Table(:,1))==current_run,:)];
						eye.samplenum=uint32(data(:,1));
						for i = 2:length(data_headers)
							negthing=1;
							if sum(abs(data(:,i)-floor(data(:,i))))==0
								typething='int';
								if min(data(:,i))>=0
									typething=['u' typething];
									negthing=0;
								end
								switch 1
									case (max(abs(data(:,i)))<2^(8-negthing))
										typething=[typething '8'];
									case (max(abs(data(:,i)))<2^(16-negthing))
										typething=[typething '16'];
									case (max(abs(data(:,i)))<2^(32-negthing))
										typething=[typething '32'];
								end
							else
								typething='single';
							end
							param=data_headers{i};
							param(param==' ')=[]; param(param=='-')='_';
							eval(['eye.' param '=' typething '(data(:,i));']);
						end
						if Runs_Recorded>1
							e(current_run)=eye;
						end
						if saveit
							if Runs_Recorded>1
								save([filename{j}(1:end-4) '('  num2str(current_run) ')_i.mat'],'eye')
							else
								save([filename{j}(1:end-4) '_i.mat'],'eye')
							end
						end
						linenum=linenum+length(data);
						current_run=current_run+1;
				end % switch case
			end % if isempty(tline)
		end % while current_run>0
		fclose(fid);
		if nargout==0
			disp(['finished: ' filename{j}]);
		end
		if Runs_Recorded>1
			eye=e;
		end
		
	end % length filename
	return
	
	
function str=tab_delim_i(tline)
	str={};
	while ~isempty(tline)
		[str{end+1}, tline] = strtok(tline, 9); %9 == 'tab'
	end
